package com.aaa.tars.controller;

import com.aaa.tars.entity.Menu;
import com.aaa.tars.entity.User;
import com.aaa.tars.service.MenuService;
import com.aaa.tars.util.Result;
import com.aaa.tars.util.ResultUtil;
import com.aaa.tars.vo.TreeNode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @FileName: MenuController
 * @Description:
 * @Author: ghz
 * @CreateTime:2025/8/29 20:27
 * @Version:1.0.0
 */

@RestController
@RequestMapping("menu")
public class MenuController extends BaseController{
    @Resource
    private MenuService menuService;
    /**
     * 查询所有树数据
     * @return
     */
    @GetMapping("queryAllTreeData")
    public Result queryAllTreeData(){
        return success(menuService.queryTreeData());
    }
    /**
     * 根据用户id查询该用户拥有的所有树数据
     * @return
     */
    @GetMapping("queryTreeDataByUserId")
    public Result queryTreeDataByUserId(HttpSession session){
        // 获取用户id
        Integer userId = ((User) session.getAttribute("userInfo")).getUserId();
        return ResultUtil.success(menuService.queryTreeDataByUserId(userId));
    }

    @GetMapping("queryById")
    public Result queryById(Integer menuId) {
        return ResultUtil.success(this.menuService.queryById(menuId));
    }

    @PostMapping("add")
    public Result add(@RequestBody Menu menu) {
        return ResultUtil.success(this.menuService.insert(menu));
    }

    @PostMapping("edit")
    public Result edit(@RequestBody Menu menu) {
        return ResultUtil.success(this.menuService.update(menu));
    }

    @DeleteMapping("deleteById")
    public Result deleteById(Integer menuId) {
        return ResultUtil.success(this.menuService.deleteById(menuId));
    }
}
